package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjcommoditytype;
import com.chezhujie.www.service.CzjcommoditytypeService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   商品类型表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjcommoditytype")
public class CzjcommoditytypeController extends BaseController<Czjcommoditytype> {
    @Autowired
    private CzjcommoditytypeService czjcommoditytypeService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjcommoditytype czjcommoditytype, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcommoditytype,req,model);
        setSupId(model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjcommoditytype);
            Pagination pagination = czjcommoditytypeService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取商品类型表列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjcommoditytype);
        return "czjcommoditytype/list";
    }
    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjcommoditytype czjcommoditytype,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjcommoditytype.getId())){
                czjcommoditytypeService.update(czjcommoditytype);


            }else{
                czjcommoditytype.setId(UUIDUtils.getUUID());
                czjcommoditytype.setCreateTime(new Date());
                czjcommoditytypeService.insert(czjcommoditytype);
            }
            model.addAttribute("success", "保存商品类型表成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存商品类型表失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String id,Czjcommoditytype czjcommoditytype, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcommoditytype,req,model);
        setSupId(model);
        try {
            Czjcommoditytype entity = czjcommoditytypeService.queryById(czjcommoditytype.getId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询商品类型表出错!详情："+e.getMessage());
        }
        return "czjcommoditytype/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjcommoditytype czjcommoditytype, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjcommoditytype,req,model);
        setSupId(model);
        if(StringUtils.isNotBlank(czjcommoditytype.getId())){
            czjcommoditytype = czjcommoditytypeService.queryById(czjcommoditytype.getId());
            model.addAttribute("entity", czjcommoditytype);
        }
        return "czjcommoditytype/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String id,Czjcommoditytype czjcommoditytype, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjcommoditytypeService.deleteById(czjcommoditytype.getId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    private void setSupId(ModelMap model) {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String,Object>> listSupId = czjcommoditytypeService.getListSupId(param);
        // map.put("name","");
        // map.put("value","");
        model.addAttribute("listSupId", listSupId);
    }

    private void setMsg(Czjcommoditytype czjcommoditytype,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjcommoditytype);
    }

    /**
     * api接口商品分类列表
     * @param pageNo
     * @param pageSize
     * @param req
     * @param resp
     * @param model
     * @throws Exception
     */
    @RequestMapping("/apiList.jn")
    public void apiList(
                        HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model) throws Exception {
      try{

            Pagination pagination = czjcommoditytypeService.getListByHasCommodity();

            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取商品分类列表失败!"+e,"",resp);
        }

    }
}
